function loading() {
  const frames = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
  const interval = 80;
  const stream = process.stderr;
  let curIdx = 0;
  const timer = setInterval(() => {
    const frame = frames[curIdx++ % frames.length];
    stream.cursorTo(0);
    stream.write(`${frame} Loading...`);
  }, interval);
  return function (err, data) {
    clearInterval(timer);
    stream.clearLine();
    stream.cursorTo(0);
    if (err) {
      stream.write(`💔 ${err}`);
    } else {
      stream.write(`💚 ${data}`);
    }
  };
}

const myLoading = loading();

setTimeout(() => {
  myLoading(null, "I'm OK!");
}, 2000);

function printProgress() {
  let i = 0;
  const timer = setInterval(() => {
    i++;
    if (i <= 10) {
      process.stderr.clearLine();
      process.stderr.cursorTo(0);
      process.stderr.write(`[${"#".repeat(i).padEnd(10)}] ${i * 10}%`);
    } else {
      clearInterval(timer);
      console.log("\nProgress complete.");
    }
  }, 500);
}
